package com.blrs.controller;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.blrs.common.core.page.PageDomain;
import com.blrs.common.core.page.TableSupport;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.page.PaginationUtil;
import com.blrs.service.procure.procure.IPurchasingService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.blrs.common.annotation.Log;
import com.blrs.common.core.controller.BaseController;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.enums.BusinessType;
import com.blrs.domain.Purchasing;
import com.blrs.common.utils.poi.ExcelUtil;
import com.blrs.common.core.page.TableDataInfo;

/**
 * 采购管理Controller
 * 
 * @author ruoyi
 * @date 2023-10-17
 */
@RestController
@RequestMapping("/procure/purchasing")
public class PurchasingController extends BaseController
{
    @Autowired
    private IPurchasingService purchasingService;

    /**
     * 查询采购管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Purchasing purchasing)
    {
        startPage();
        purchasing.setIsCooperate("0");
        List<Purchasing> list = purchasingService.selectPurchasingList(purchasing);
        return getDataTable(list);
    }

    /**
     * 查询合作管理列表
     */
    @GetMapping("/listCooperate")
    public TableDataInfo listCooperate(Purchasing purchasing)
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        purchasing.setIsCooperate("1");
        List<Purchasing> list = purchasingService.selectPurchasingList(purchasing);
        return PaginationUtil.getPageInfo(list, pageNum, pageSize);
    }

    /**
     * 导出采购管理列表
     */
    @Log(title = "采购管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Purchasing purchasing)
    {
        purchasing.setIsCooperate("0");
        List<Purchasing> list = purchasingService.selectPurchasingList(purchasing);
        ExcelUtil<Purchasing> util = new ExcelUtil<Purchasing>(Purchasing.class);
        util.exportExcel(response, list, "采购管理数据");
    }

    /**
     * 导出合作管理列表
     */
    @PostMapping("/exportCooperate")
    public void exportCooperate(HttpServletResponse response, Purchasing purchasing)
    {
        purchasing.setIsCooperate("1");
        List<Purchasing> list = purchasingService.selectPurchasingList(purchasing);
        ExcelUtil<Purchasing> util = new ExcelUtil<Purchasing>(Purchasing.class);
        util.exportExcel(response, list, "合作管理数据");
    }



    /**
     * 获取采购管理和合作管理详情信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(purchasingService.selectPurchasingById(id));
    }

    /**
     * 新增采购管理
     */
    @Log(title = "采购管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Purchasing purchasing) throws NoSuchFieldException, IllegalAccessException {
        return toAjax(purchasingService.insertPurchasing(purchasing));
    }

    /**
     * 修改采购管理
     */
    @Log(title = "采购管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@RequestBody Purchasing purchasing) throws NoSuchFieldException, IllegalAccessException {
        return toAjax(purchasingService.updatePurchasing(purchasing));
    }

    /**
     * 修改采购管理
     */
    @Log(title = "采购管理", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult updateCooperate(@RequestBody Purchasing purchasing) throws NoSuchFieldException, IllegalAccessException {
        return toAjax(purchasingService.updatePurchasingCooperate(purchasing));
    }



    /**
     * 删除采购管理
     */
    @Log(title = "采购管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(purchasingService.deletePurchasingByIds(ids));
    }

    @GetMapping("/end/{id}")
    public AjaxResult end(@PathVariable String id){
        return toAjax(purchasingService.updatePurchasingStatus(id));
    }

    //获取最新生成编号
    @GetMapping("/LatestNo")
    public AjaxResult LastNo(){
        return AjaxResult.success(purchasingService.findLastNo());
    }

    //查看待审核的采购信息
    @GetMapping("/listByExamine")
    public TableDataInfo listByExamine(Purchasing purchasing)
    {
        startPage();
        purchasing.setIsCooperate("0");
        purchasing.setExamineId(SecurityUtils.getUserId());
        List<Purchasing> list = purchasingService.selectPurchasingListExamine(purchasing);
        return getDataTable(list);
    }
    //审核通过
    @GetMapping("/Approved/{id}")
    public AjaxResult Approved(@PathVariable String id){
     return   toAjax( purchasingService.updatePurchasingExamineStatus(id));
    }

    //审核打回
    @GetMapping("/repulse/{id}")
    public AjaxResult repulse(@PathVariable String id){
        return   toAjax( purchasingService.repulse(id));
    }

    //查看待审核的合作管理
    @GetMapping(value = "/listByCooperateExamine")
    public TableDataInfo listByCooperate(Purchasing purchasing) {
        startPage();
        purchasing.setIsCooperate("1");
        purchasing.setExamineId(SecurityUtils.getUserId());
        List<Purchasing> list = purchasingService.selectPurchasingListExamine(purchasing);
        return getDataTable(list);
    }
}
